package com.alipay.api.test.fsf;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayDataDataserviceBillDownloadurlQueryRequest;
import com.alipay.api.response.AlipayDataDataserviceBillDownloadurlQueryResponse;

public class AlipayBill {
     //初始化实例请求对象     
    public static String APP_ID = "2016091200495918";   //自己的商户appid
   // RSA签名方式使用算法：SHA1withRSA。 注意此处是RSA 的私钥 是Java版本格式的 如果是用.NET 就要转换为.NET版本的格式
    public static String APP_PRIVATE_KEY = "ghjjjjffjjjjkytyyddfsgsaetertrtr..............";  
    
   //RSA2签名方式使用算法：SHA256withRSA。
    //public static String APP_PRIVATE_KEY ="+zA3RJzhtiL4MIHks4GD++6RuQ/";
    
    //公钥是可以公用的在公共平台配置的 .NET和Java语言的都可以共用。但是要区别 RSA或者是RSA2的公钥签名
      public static String ALIPAY_PUBLIC_KEY = "/fEsraprwCtzvzQETrNRwmZkh2QhUrCmZYI"; 
      //rsa2
      // public static String ALIPAY_PUBLIC_KEY ="/aFKRhSS5fHChIzjpMrl";
    
    //开放平台SDK封装了签名实现，只需在创建DefaultAlipayClient对象时，设置请求网关(gateway)，应用id(app_id)，应用私钥(private_key)，编码格式(charset)，支付宝公钥(alipay_public_key)，签名类型(sign_type)即可，报文请求时会自动进行签名。
    public static AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", APP_ID, APP_PRIVATE_KEY, "json", "GBK", 
                    ALIPAY_PUBLIC_KEY,"RSA");  //注意在此处用的RSA签名类型参数 如果是RSA2的就要传RSA2参数
     public static String  post_bill_date="2017-06-20"  ;
    
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        bill_download_url ();//下载对账单接口 
    }
    public static void  bill_download_url (){ 
      //"  \"bill_date\":\"2017-07-26\""+      时间参数  "+post_bill_date+"
    //账单时间：日账单格式为yyyy-MM-dd，月账单格式为yyyy-MM。 
    //账单参数类型 值 signcustomer  包含 是账务明细的 也有业务明细的  trade 只有 是业务明细的   在这里特别注意下 以免被挖坑了 看官网的容易坑
        AlipayDataDataserviceBillDownloadurlQueryRequest request = new AlipayDataDataserviceBillDownloadurlQueryRequest(); 
                request.setBizContent("{" + 
                "  \"bill_type\":\"trade\"," + 
                "  \"bill_date\":\"2017-07-26\""+ 
                "}"); 
                AlipayDataDataserviceBillDownloadurlQueryResponse response = null; 
               try { 
                       response = alipayClient.execute(request); 
                       System.out.println(response.getBillDownloadUrl());   
                        
               } 
               catch (AlipayApiException e)
               { 
                       e.printStackTrace(); 
               } 
       //调用成功，则处理业务逻辑
       if(response.isSuccess())
         { 
         //将接口返回的对账单下载地址传入urlStr  
         String urlStr=response.getBillDownloadUrl(); 
        //指定希望保存的文件路径 
         String filePath = "F:\\fund_bill_20170726.csv.zip"; 
         URL url = null; 
         HttpURLConnection httpUrlConnection = null; 
         InputStream fis = null; 
         FileOutputStream fos = null; 
         try { 
             url = new URL(urlStr); 
             httpUrlConnection = (HttpURLConnection) url.openConnection(); 
             httpUrlConnection.setConnectTimeout(5 * 1000); 
             httpUrlConnection.setDoInput(true); 
             httpUrlConnection.setDoOutput(true); 
             httpUrlConnection.setUseCaches(false); 
             httpUrlConnection.setRequestMethod("GET"); 
             httpUrlConnection.setRequestProperty("CHARSET", "UTF-8"); 
             httpUrlConnection.connect(); 
             fis = httpUrlConnection.getInputStream(); 
             byte[] temp = new byte[1024]; 
             int b; 
             fos = new FileOutputStream(new File(filePath)); 
             while ((b = fis.read(temp)) != -1) { 
                 fos.write(temp, 0, b); 
                 fos.flush(); 
             } 
         } catch (MalformedURLException e) { 
             e.printStackTrace(); 
         } catch (IOException e) { 
             e.printStackTrace(); 
         } finally { 
             try { 
                 if(fis!=null) fis.close(); 
                 if(fos!=null) fos.close(); 
                 if(httpUrlConnection!=null) httpUrlConnection.disconnect(); 
             } catch (IOException e) { 
                 e.printStackTrace(); 
             } 
         } 
                
         } 
       } 
}